package com.ideal.frame.convert;

import com.ideal.frame.util.DtoUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

public class EnumConvertFactory implements ConverterFactory<String, Enum> {

    public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
        return new EnumConvertFactory.StringToEnum(targetType);
    }

    private class StringToEnum<T extends Enum> implements Converter<String, T> {
        private Class<T> enumType;

        public StringToEnum(Class<T> var1) {
            this.enumType = var1;
        }

        public T convert(String source) {
            if (StringUtils.isEmpty(source)) {
                return null;
            }
            return DtoUtils.getEnumByName(this.enumType, source);
        }
    }

}
